package com.slg.android.manyanime.bean;

import android.view.animation.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by E540 on 2015/3/12.
 */
public class AnimationDetail {
    /**
     * 视频id
     */
    private String videoId;
    /**
     * 名称
     */
    private String name;
    /**
     * 简介
     */
    private String intro;
    /**
     * 地区
     */
    private String area;
    /**
     * 图片
     */
    private String cover;
    /**
     * 主演
     */
    private String character;
    /**
     * 是否完结 1完结0未完结
     */
    private String isOver;
    /**
     * 当前更新至
     */
    private String curNum;
    /**
     * 共多少集
     */
    private String totalNum;
    /**
     * 类型
     */
    private String category;
    /**
     * 导演
     */
    private String director;
    /**
     * 得分
     */
    private String score;
    /**
     * 更新时间
     */
    private String updateTime;
    /**
     * 发布时间
     */
    private String publishTime;
    /**
     * 视频源
     */
    private List<FromSite> fromSites;
    /**
     * 赞
     */
    private String praiseNum;
    private List<WatchUser> watchUsers;
    private String commentNum;
    private String scoreTotalNum;
    private String scorePersonNum;
    private String playType;
    private List<Episode> episodes;
    private List<Category> categories;
    private String updateString;
    private boolean canDownload;

    public static AnimationDetail parseData(String json) {
        AnimationDetail detail = new AnimationDetail();
        try {
            JSONObject data = new JSONObject(json);
            detail.setVideoId(data.getString("video_id"));
            detail.setName(data.getString("name"));
            detail.setIntro(data.getString("intro"));
            detail.setArea(data.getString("area"));
            detail.setCover(data.getString("cover"));
            detail.setCharacter(data.getString("character"));
            detail.setIsOver(data.getString("is_over"));
            detail.setCurNum(data.getString("cur_num"));
            detail.setTotalNum(data.getString("total_num"));
            detail.setCategory(data.getString("category"));
            detail.setDirector(data.getString("director"));
            detail.setScore(data.getString("score"));
            detail.setUpdateTime(data.getString("update_time"));
            detail.setPublishTime(data.getString("publish_time"));

            JSONArray from_site = data.optJSONArray("from_site");
            List<FromSite> fromSiteList = new ArrayList<>();
            for (int i = 0; i < from_site.length(); i++) {
                JSONObject jsonObject = from_site.getJSONObject(i);
                FromSite fromSite = new FromSite();
                fromSite.setId(jsonObject.getString("id"));
                fromSite.setFrom(jsonObject.getString("from"));
                fromSite.setName(jsonObject.getString("name"));
                fromSiteList.add(fromSite);
            }
            detail.setFromSites(fromSiteList);

            detail.setPraiseNum(data.optString("praiseNum"));

            JSONArray watch_User = data.optJSONArray("watchUser");
            List<WatchUser> watchUserList = new ArrayList<>();
            for (int i = 0; i < watch_User.length(); i++) {
                JSONObject jsonObject = watch_User.getJSONObject(i);
                WatchUser watchUser = new WatchUser();
                watchUser.setId(jsonObject.getString("id"));
                watchUser.setUid(jsonObject.getString("uid"));
                watchUser.setHeadPic(jsonObject.getString("head_pic"));
                watchUser.setNickName(jsonObject.getString("nickname"));
                watchUser.setHxUsername(jsonObject.getString("hx_username"));
                watchUser.setSuperscript(jsonObject.getString("superscript"));
                watchUser.setSex(jsonObject.getString("sex"));
                watchUserList.add(watchUser);
            }
            detail.setWatchUsers(watchUserList);

            detail.setCommentNum(data.optString("comment_num"));
            detail.setScoreTotalNum(data.optString("score_total_num"));
            detail.setScorePersonNum(data.optString("score_person_num"));
            detail.setPlayType(data.optString("playType"));

            JSONArray episodes = data.optJSONArray("episode");
            List<Episode> episodeList = new ArrayList<>();
            for (int i = 0; i < episodes.length(); i++) {
                JSONObject jsonObject = episodes.getJSONObject(i);
                Episode episode = new Episode();
                episode.setId(jsonObject.getString("id"));
                episode.setName(jsonObject.getString("name"));
                episode.setFromSite(jsonObject.getString("from_site"));
                episode.setVid(jsonObject.getString("vid"));
                episode.setUrl(jsonObject.getString("url"));
                episode.setPlayType(jsonObject.getString("playType"));
                episode.setmUrl(jsonObject.getString("mUrl"));
                episode.setNeedHtml5(jsonObject.getBoolean("need_html5"));
                episode.setHave(jsonObject.getBoolean("have"));
                episodeList.add(episode);
            }
            detail.setEpisodes(episodeList);

            JSONArray categories = data.optJSONArray("categorys");
            ArrayList<Category> categoryList = new ArrayList<>();
            for (int i = 0; i < categories.length(); i++) {
                JSONObject jsonObject = categories.getJSONObject(i);
                Category c = new Category();
                c.setId(jsonObject.getString("id"));
                c.setName(jsonObject.getString("name"));
                categoryList.add(c);
            }
            detail.setCategories(categoryList);

            detail.setUpdateString(data.optString("update_string"));
            detail.setCanDownload(data.optBoolean("true"));
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return detail;
    }


    //==================getter && setter==================

    public String getVideoId() {
        return videoId;
    }

    public void setVideoId(String videoId) {
        this.videoId = videoId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getCharacter() {
        return character;
    }

    public void setCharacter(String character) {
        this.character = character;
    }

    public String getIsOver() {
        return isOver;
    }

    public void setIsOver(String isOver) {
        this.isOver = isOver;
    }

    public String getCurNum() {
        return curNum;
    }

    public void setCurNum(String curNum) {
        this.curNum = curNum;
    }

    public String getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(String totalNum) {
        this.totalNum = totalNum;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }

    public List<FromSite> getFromSites() {
        return fromSites;
    }

    public void setFromSites(List<FromSite> fromSites) {
        this.fromSites = fromSites;
    }

    public String getPraiseNum() {
        return praiseNum;
    }

    public void setPraiseNum(String praiseNum) {
        this.praiseNum = praiseNum;
    }

    public List<WatchUser> getWatchUsers() {
        return watchUsers;
    }

    public void setWatchUsers(List<WatchUser> watchUsers) {
        this.watchUsers = watchUsers;
    }

    public String getCommentNum() {
        return commentNum;
    }

    public void setCommentNum(String commentNum) {
        this.commentNum = commentNum;
    }

    public String getScoreTotalNum() {
        return scoreTotalNum;
    }

    public void setScoreTotalNum(String scoreTotalNum) {
        this.scoreTotalNum = scoreTotalNum;
    }

    public String getScorePersonNum() {
        return scorePersonNum;
    }

    public void setScorePersonNum(String scorePersonNum) {
        this.scorePersonNum = scorePersonNum;
    }

    public String getPlayType() {
        return playType;
    }

    public void setPlayType(String playType) {
        this.playType = playType;
    }

    public List<Episode> getEpisodes() {
        return episodes;
    }

    public void setEpisodes(List<Episode> episodes) {
        this.episodes = episodes;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public String getUpdateString() {
        return updateString;
    }

    public void setUpdateString(String updateString) {
        this.updateString = updateString;
    }

    public boolean isCanDownload() {
        return canDownload;
    }

    public void setCanDownload(boolean canDownload) {
        this.canDownload = canDownload;
    }
}
